<template>
  <div class="login-container">
    <el-card class="login-card">
      <h1 class="login-title">少儿编程学员管理系统登录</h1>
      <div class="login-form">
        <el-form :model="loginForm" ref="ruleFormRef" :rules="rules" hide-required-asterisk>
          <el-row>
            <el-col :span="24">
              <el-form-item label="用户名" label-width="60px" prop="username">
                <el-input v-model="loginForm.username" autocomplete="off" prefix-icon="user" maxlength="10"
                  show-word-limit></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="密 码" label-width="60px" prop="password">
                <el-input type="password" v-model="loginForm.password" autocomplete="off" prefix-icon="lock"
                  show-password></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="handleLogin(ruleFormRef)" style="width: 100%;">登录</el-button>
          </el-form-item>
        </el-form>
        <RouterView />
      </div>
      <!-- 无帐号，点此注册 -->
      <el-row style="text-align: right;">
        <el-col>
          <el-text class="w-150px mb-2">
            若无帐号，点此>> <RouterLink to="/register" style="font-size: 1.2em;">注册</RouterLink>
          </el-text>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { RouterLink } from 'vue-router'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus';
import axios from 'axios'

const loginForm = ref({
  username: '',
  password: ''
});
const ruleFormRef = ref()
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

const router = useRouter()
async function handleLogin(formEl) {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        const res = await axios.post('http://localhost:8080/api/login', loginForm.value)
        if (res.data.code === 200) {
          ElMessage.success('登录成功')
          // 可选：保存token到localStorage
          // localStorage.setItem('token', res.data.token)
          router.push('/dashboard')
        } else {
          ElMessage.error(res.data.msg || '用户名或密码错误')
        }
      } catch (e) {
        ElMessage.error('登录失败，请检查网络或联系管理员')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/login1.jpg');
  background-size: cover;
  background-position: center;
}

.login-card {
  width: 420px;
  padding: 20px;
  background-color: #fefefe;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #409eff;
}

.login-form {
  width: 100%;
}
</style>